package com.lj.msm.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import com.lj.msm.service.MsmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;


/**
 * @author luojing
 * @version 1.0
 * @date 2021/6/21 11:53
 */
@Service
public class MsmServiceImpl implements MsmService {

    @Autowired
    MailSender mailSender;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //    @Override
    //    public Boolean send(String code, String phone) {
    //
    //        if (StringUtils.isEmpty(phone)){
    //            return false;
    //        }
    //        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI4FvvVEWiTJ3GNJJqJnk7", "9st82dv7EvFk9mTjYO1XXbM632fRbG");
    //        IAcsClient client = new DefaultAcsClient(profile);
    //
    //        //固定参数
    //        CommonRequest request = new CommonRequest();
    //        request.setSysMethod(MethodType.POST);
    //        request.setSysDomain("dysmsapi.ap-southeast-1.aliyuncs.com");
    //        request.setSysVersion("2017-05-25");
    //        request.setSysAction("SendSms");
    //        //设置发送参数
    //        request.putQueryParameter("PhoneNumbers", phone);
    //        request.putQueryParameter("SignName", "我的谷粒在线教育网站");
    //        request.putQueryParameter("TemplateCode", "SMS_180051135");
    //        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
    //
    //        //发送
    //        try {
    //            CommonResponse response = client.getCommonResponse(request);
    //            System.out.println(response.getData());
    //            return response.getHttpResponse().isSuccess();
    //        } catch (ClientException e) {
    //            e.printStackTrace();
    //            return false;
    //        }
    //
    //    }

    /**
     * 由于阿里的签名还没有申请下来，暂时用邮箱发送
     *
     * @param code
     * @param phone 邮箱
     * @return
     */
    @Override
    public Boolean send(String code, String phone) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("验证码");
        simpleMailMessage.setText(String.format("您的验证码为：%s，请不要告诉其他人，五分钟内有效哦~", code));
        simpleMailMessage.setTo(phone);
        simpleMailMessage.setFrom("1126184155@qq.com");

        String s = redisTemplate.opsForValue().get(phone);
        if (!StringUtils.isEmpty(s)){
            return true;
        }

        try {
            mailSender.send(simpleMailMessage);
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
